﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//引入命名空间
using System.Data;

public partial class yggengxin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYear();
            BindMonth();
            BindDay();
            BindProvince();
            ReadInfo();
        }
    }
    protected void ReadInfo()//把用户的个人信息读取出来
    {
        string num = "";

        if (Request.QueryString["Snum"] != null)
        {
            num = Request.QueryString["Snum"].ToString().Trim();
        }

        // if (Session["tmpID"] != null)
        // {
        //    id = Session["tmpID"].ToString();
        // }
        txtNum1.Text = num;

        staff tmp = new staff();
        tmp.num = num;

        SqlDataReader rd = tmp.Getonestaff(tmp);

        while (rd.Read())
        {
            txtPw1.Text = rd["Spw"].ToString().Trim();
            txtName1.Text = rd["Sname"].ToString().Trim();
            string birth = rd["Sbirth"].ToString().Trim();
            if (birth.Length != 0)
            {
                string year = birth.Substring(0, 4);
                int month = Convert.ToInt32(birth.Substring(4, 2));
                int day = Convert.ToInt32(birth.Substring(6, 2));
                drpYear1.SelectedValue = year;
                drpMonth1.SelectedValue = month.ToString();
                drpDay1.SelectedValue = day.ToString();
            }

            string proid = rd["Sprovince"].ToString().Trim();
            if (proid.Length != 0)
            {
                drpProvince1.SelectedValue = proid;
                BindCity(drpProvince1.SelectedValue);
                string cityid = rd["Scity"].ToString().Trim();
                if (cityid.Length != 0)
                {
                    drpCity1.SelectedValue = cityid;
                }
            }

            txtQq1.Text = rd["Sqq"].ToString().Trim();
            txtMail1.Text = rd["Smail"].ToString().Trim();
            txtPhone1.Text = rd["Sphone"].ToString().Trim();
            txtAddress1.Text = rd["Saddress"].ToString().Trim();
            txtIdcard1.Text = rd["Sidcard"].ToString().Trim();
            string problem = rd["Sproblem"].ToString().Trim();
            if (problem.Length != 0)
            {
                drpProblem1.SelectedValue = problem;
            }

            txtAnswer1.Text = rd["Sanswer"].ToString().Trim();

        }
        rd.Close();
    }
    protected void drpProblem_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpProblem1.SelectedIndex == 3)
        {
            txtProblem1.Visible = true;
        }
        else
        {
            txtProblem1.Visible = false;
        }
    }
    protected void BindYear()
    {
        drpYear1.Items.Clear();
        int startYear = DateTime.Now.Year - 10;
        int currentYear = DateTime.Now.Year;
        for (int i = startYear; i <= currentYear; i++)
        {
            drpYear1.Items.Add(new ListItem(i.ToString()));
        }
        drpYear1.SelectedValue = currentYear.ToString();
    }
    protected void BindMonth()
    {
        drpMonth1.Items.Clear();
        for (int i = 1; i <= 12; i++)
        {
            drpMonth1.Items.Add(i.ToString());
        }
    }
    protected void BindDay()
    {
        drpDay1.Items.Clear();
        string year = drpYear1.SelectedValue;
        string month = drpMonth1.SelectedValue;
        int days = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));
        for (int i = 1; i <= days; i++)
        {
            drpDay1.Items.Add(i.ToString());
        }
    }


    protected void BindProvince()
    {
        staff tmp = new staff();
        drpProvince1.DataSource = tmp.BindProvince().Tables[0].DefaultView;
        drpProvince1.DataTextField = "FName";
        drpProvince1.DataValueField = "FID";
        drpProvince1.DataBind();
    }

    protected void BindCity(string proid)
    {
        staff tmp = new staff();
        drpCity1.DataSource = tmp.BindCity(proid).Tables[0].DefaultView;
        drpCity1.DataValueField = "FcityID";
        drpCity1.DataTextField = "FcityName";
        drpCity1.DataBind();
    }
    protected void drpProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCity(drpProvince1.SelectedValue.Trim());
    }

    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDay();
    }
    protected void drpMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDay();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string num = txtNum1.Text.Trim();
        string name = txtName1.Text.Trim();
        string pw = txtPw1.Text.Trim();
        string year = drpYear1.SelectedValue.Trim();
        string month = drpMonth1.SelectedValue.Trim();
        string day = drpDay1.SelectedValue.Trim();
        string bitrh = "";
        if (month.Length == 1)
        {
            month = "0" + month;
        }
        if (day.Length == 1)
        {
            day = "0" + day;
        }
        bitrh = year + month + day;


        string proid = drpProvince1.SelectedValue.Trim();
        string cityid = drpCity1.SelectedValue.Trim();
        string qq = txtQq1.Text.Trim();
        string phone = txtPhone1.Text.Trim();
        string mail = txtMail1.Text.Trim();
        string address = txtAddress1.Text.Trim();
        string problem = txtProblem1.Text.Trim();
        string answer = txtAnswer1.Text.Trim();
        string idcard = txtIdcard1.Text.Trim();

        staff tmp = new staff();
        tmp.num = num;
        tmp.name = name;
        tmp.pw = pw;
        tmp.birth = bitrh;
        tmp.province = proid;
        tmp.city = cityid;
        tmp.qq = qq;
        tmp.mail = mail;
        tmp.phone = phone;
        tmp.address = address;
        tmp.problem = problem;
        tmp.answer = answer;
        tmp.idcard = idcard;
        int result = tmp.Updateonestaff(tmp);
        if (result > 0)
        {
            Response.Write("<script> alert(\"更新个人资料成功！\")</script>");
        }

        else
        {
            Response.Write("<script> alert(\"更新个人资料失败！\")</script>");
        }
    }
}